package com.jky.trial.controller;

import com.jky.trial.common.BaseResult;
import com.jky.trial.common.Constants;
import com.jky.trial.common.ListResult;
import com.jky.trial.common.jwt.JWTUtil;
import com.jky.trial.entity.Device;
import com.jky.trial.entity.User;
import com.jky.trial.service.DeviceService;
import com.jky.trial.vo.DeviceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/api/v1/device")
public class DeviceController {

    private DeviceService recordService;

    @Autowired
    DeviceController(DeviceService recordService) {

        this.recordService = recordService;
    }

    @PostMapping
    public BaseResult create(@RequestBody Device record, HttpServletRequest request) {
        User user = JWTUtil.getUser(request.getHeader("tk"));
        record.setCreatorId(user.getId().toString());
        recordService.create(record);
        return new BaseResult(true, record.getId());
    }

    @PutMapping
    public BaseResult update(@RequestBody Device record) {
        return new BaseResult(true, recordService.update(record));
    }

    @DeleteMapping("/{id}")
    public BaseResult del(@PathVariable Integer id) {
        return new BaseResult(true, recordService.del(id));
    }

    @GetMapping("/{id}")
    public BaseResult get(@PathVariable Integer id) {
        return new BaseResult(true, recordService.detail(id));
    }

    @GetMapping
    public ListResult page(DeviceVo record) {
        return recordService.page(record);
    }

    @GetMapping("/list")
    public ListResult list(DeviceVo record) {
        return new ListResult(true, recordService.list(record));
    }

    @GetMapping("/listWithCreatorName")
    public ListResult listWithCreatorName(DeviceVo record) {
        return recordService.pageWithCreatorName(record);
    }
}
